package com.guashu.jaywayjsonpath.jasonPath;

import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Option;
import net.minidev.json.JSONArray;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class JsonPathExample {
    public static void main(String[] args) {
        String json = JsonStr.getJsonString();

        // 使用默认配置创建一个配置实例，这将自动处理null值和空集合
        Configuration conf = Configuration.defaultConfiguration()
                .addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL, Option.ALWAYS_RETURN_LIST);

        // 解析规则
        Map<String, String> fieldJsonPaths = new HashMap<>();
        fieldJsonPaths.put("pa01bi01", "$.PRH.PA01.PA01B.PA01BI01");
        fieldJsonPaths.put("pd01ad01", "$.PDA.PD01[*].PD01A.PD01AD01");
        fieldJsonPaths.put("pd01bd01", "$.PDA.PD01[*].PD01B.PD01BD01");
        fieldJsonPaths.put("pd01ai01", "$.PDA.PD01[*].PD01A.PD01AI01");
        fieldJsonPaths.put("pd01er03", "$.PDA.PD01[*].PD01E.PD01EH[*].PD01ER03");
        fieldJsonPaths.put("pd01ed01", "$.PDA.PD01[*].PD01E.PD01EH[*].PD01ED01");
        fieldJsonPaths.put("pd01ej01", "$.PDA.PD01[*].PD01E.PD01EH[*].PD01EJ01");

        List<String> strList = new ArrayList<>();

        // 对于每个规则，使用JSONPath解析JSON数据
        String pa01bi01 = JsonPath.using(conf).parse(json).read(fieldJsonPaths.get("pa01bi01"), String.class);

        List<String> pd01ad01 = JsonPath.using(conf).parse(json).read(fieldJsonPaths.get("pd01ad01"));
        List<String> pd01bd01 = JsonPath.using(conf).parse(json).read(fieldJsonPaths.get("pd01bd01"));
        List<String> pd01ai01 = JsonPath.using(conf).parse(json).read(fieldJsonPaths.get("pd01ai01"));
        List<List<String>> pd01er03 = JsonPath.using(conf).parse(json).read(fieldJsonPaths.get("pd01er03"));
        List<List<String>> pd01ed01 = JsonPath.using(conf).parse(json).read(fieldJsonPaths.get("pd01ed01"));
        List<List<String>> pd01ej01 = JsonPath.using(conf).parse(json).read(fieldJsonPaths.get("pd01ej01"));

        // 遍历所有的组合并添加到strList中
        for (int i = 0; i < pd01ad01.size(); i++) {
            for (int j = 0; j < pd01er03.get(i).size(); j++) {
                String str = pa01bi01 + "," +
                        pd01ad01.get(i) + "," +
                        pd01bd01.get(i) + "," +
                        pd01ai01.get(i) + "," +
                        pd01er03.get(i).get(j) + "," +
                        pd01ed01.get(i).get(j) + "," +
                        pd01ej01.get(i).get(j);
                strList.add(str);
            }
        }

        // 打印结果
        for (String s : strList) {
            System.out.println(s);
        }
    }
}
